package com.xj.driver.mqtt.api;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONObjectCodec;
import com.google.gson.annotations.JsonAdapter;
import com.xj.common.bean.R;
import com.xj.common.bean.constant.Common;
import com.xj.common.bean.em.ControlStateEm;
import com.xj.common.model.ControlInfo;
import com.xj.common.sdk.bean.command.CmdParameter;
import com.xj.common.sdk.service.ControlService;
import com.xj.common.sdk.service.DriverCommandService;
import com.xj.common.utils.JsonPaseUtil;
import com.xj.common.valid.ValidatableList;
import com.xj.driver.mqtt.bean.CtlBodyB;
import com.xj.driver.mqtt.bean.MqttHeaderB;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *驱动命令对外暴露接口
 * @author wsp
 *
 */
@RestController
@RequestMapping(value = "/ctl")
public class ControlAPI {

	@Resource
	private DriverCommandService driverCommandService;
	@Resource
	private ControlService controlService;


	@RequestMapping(value = "/sendCtl",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	public Object handleSendControl(
			@RequestBody JSONObject jsonParam
	) throws IOException
	{
		//CtlBodyB cbb = JsonPaseUtil.JSONStrToObj(jsonParam.getJSONObject("header").toJSONString(),CtlBodyB.class);
		//MqttHeaderB mhb = JsonPaseUtil.JSONStrToObj(jsonParam.getJSONObject("dataBody").toJSONString(),MqttHeaderB.class);
		String deviceSN = jsonParam.getString("sn");
		//String deviceid = jsonParam.getString("deviceid");
		jsonParam.remove("sn");
		String clientid = jsonParam.getString("clientid");
		//Map<String ,Object >  dataMap= new HashMap<>();
		//dataMap.put("header",jsonParam.getJSONObject("header").toJSONString());
		//dataMap.put("dataBody",jsonParam.getJSONObject("dataBody").toJSONString());
		//JSONObject json= new JSONObject(dataMap);
		Long handle = controlService.snedCtl(jsonParam.toJSONString(),null,deviceSN,clientid);
		//controlService.snedCtl(jsonParam,null,null,deviceid);
		//return R.ok(jsonParam.getJSONObject("header").getInteger("index"));

		return R.ok(handle);
	}

	@RequestMapping(value = "/sendState",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	public Object handleSendState(
			@RequestBody JSONObject jsonParam
	) throws IOException
	{
		String cbb = jsonParam.getJSONObject("dataBody").toJSONString();
		//MqttHeaderB mhb = JsonPaseUtil.JSONStrToObj(jsonParam.getJSONObject("header").toJSONString(),MqttHeaderB.class);
		String deviceid = jsonParam.getString("deviceid");
		controlService.snedState(jsonParam.toJSONString(),null,deviceid);
		return R.ok();

	}



	@RequestMapping(value = "/send",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	public Object handleSendControl_new(
			@RequestBody JSONObject jsonParam
	) throws IOException
	{
		String deviceSN = jsonParam.getString("sn");
		String clientID = jsonParam.getString("clientid");
		String parameter = jsonParam.getJSONObject("parameters").toJSONString();
		ControlInfo controlInfo = new ControlInfo(deviceSN, clientID, Integer.valueOf(ControlStateEm.CTL_STATE_RECEIVED.getCode()), parameter);
		controlService.sendCtl(controlInfo);

		return R.ok(controlInfo.getId());
	}

}
